package com.yisu.auction.api.web.controller;

import com.alibaba.fastjson.JSON;
import com.yisu.auction.api.core.service.INewActivitiesService;
import com.yisu.auction.api.core.service.IPrizeService;
import com.yisu.auction.api.core.service.ISystemConfigService;
import com.yisu.auction.api.domain.request.BasePage;
import com.yisu.auction.api.domain.request.activityPackage.NewActivitiesRequest;
import com.yisu.auction.api.domain.request.rechargePackage.RechargeRequest;
import com.yisu.auction.api.domain.response.activityPackage.NewActivitiesResponse;
import com.yisu.auction.api.domain.vo.activityPackageVos.NewActivitiesRechargeRuleVo;
import com.yisu.auction.api.domain.vo.activityPackageVos.NewActivitiesVo;
import com.yisu.auction.api.domain.vo.indexPackageVos.SystemConfigVo;
import com.yisu.auction.api.web.interceptor.IAuthVerifyAnnotation;
import com.yisu.auction.api.web.utils.PingppUtil;
import com.yisu.auction.ewf.common.enums.LoginTypeEnum;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.Prize;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.function.Predicate1;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：新手活动业务
 * createTime：2017/9/9
 */
@Controller
@RequestMapping(value = "/newActivities")
public class NewActivitiesController extends BaseController {
    @Resource
    private INewActivitiesService iNewActivitiesService;
    @Resource
    private ISystemConfigService iSystemConfigService;
    @Resource
    private IPrizeService iPrizeService;

    /**
     * author: 赵杰
     * Business Description：获取新手不中包赔专区竞品详情
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/getPrizeForNewActivities", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
    public JsonResult getPrizeForNewActivities(BasePage basePage) {
        NewActivitiesResponse newActivitiesResponse = new NewActivitiesResponse();
        Prize prize = iNewActivitiesService.getNewActivitiesPrize();
        if (prize == null) {
            return new JsonResult(ResponseCode.FAILURE.code, "未检测到新人活动商品");
        }
        NewActivitiesVo newActivitiesVo = new NewActivitiesVo();
        newActivitiesVo.setPrizeId(prize.getId());
        newActivitiesVo.setPrice(prize.getTransactionPrice());
        newActivitiesVo.setTransactionPrice(prize.getTransactionPrice() + "");
        if (!StringUtils.isEmpty(prize.getImage())) {
            newActivitiesVo.setImage(Linq4j.asEnumerable(prize.getImage().split(",")).toList().get(0));
        }
        newActivitiesVo.setName(prize.getName());
        SystemConfigVo configValue = iSystemConfigService.getSystemConfigByKey("NewActivitiesRechargeRule");
        List<NewActivitiesRechargeRuleVo> newUserRechargeVos = JSON.parseArray(configValue.getConfigValue(), NewActivitiesRechargeRuleVo.class);
        newActivitiesVo.setActivitiesRechargeRuleVos(newUserRechargeVos);
        newActivitiesVo.setUserAgreement("http://116.62.239.171:8500/resources/html/userAgreement.html");
        newActivitiesResponse.setNewActivitiesVo(newActivitiesVo);
        return new JsonResult(newActivitiesResponse);
    }


    /**
     * author: 赵杰
     * Business Description：新手不中包赔充值
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/newActivitiesRecharge", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult newActivitiesRecharge(final NewActivitiesRequest newActivitiesRequest) throws InterruptedException, UnsupportedEncodingException {
        if (!this.getAllowAuction()) {
            return new JsonResult(ResponseCode.FAILURE.code, "当前时段为禁拍时间！");
        }
        if (StringUtils.isEmpty(newActivitiesRequest.getAmount()) || newActivitiesRequest.getPayType() == null) {
            return new JsonResult(ResponseCode.FAILURE.code, "参数不完整");
        }
        this.userToken = newActivitiesRequest.getUserToken();
        newActivitiesRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        Prize prize = iPrizeService.getPrizeModelByPrizeId(newActivitiesRequest.getPrizeId());
        if (prize == null) {
            return new JsonResult(-1, "非法请求");
        }
        SystemConfigVo configValue = iSystemConfigService.getSystemConfigByKey("NewActivitiesRechargeRule");
        List<NewActivitiesRechargeRuleVo> newUserRechargeVos = JSON.parseArray(configValue.getConfigValue(), NewActivitiesRechargeRuleVo.class);
        if (CollectionUtils.isEmpty(newUserRechargeVos)) {
            return new JsonResult(-1, "非法请求");
        }
        NewActivitiesRechargeRuleVo newActivitiesRechargeRuleVo = Linq4j.asEnumerable(newUserRechargeVos).where(new Predicate1<NewActivitiesRechargeRuleVo>() {
            @Override
            public boolean apply(NewActivitiesRechargeRuleVo newActivitiesRechargeRuleVo) {
                return newActivitiesRechargeRuleVo.getRechargeAmount().equals(newActivitiesRequest.getAmount());
            }
        }).firstOrDefault();
        if (newActivitiesRechargeRuleVo == null) {
            return new JsonResult(-1, "非法请求");
        }
        RechargeRequest rechargeRequest = new RechargeRequest();
        rechargeRequest.setPayType(newActivitiesRequest.getPayType());
        rechargeRequest.setAmount(newActivitiesRequest.getAmount());
        rechargeRequest.setDescription(newActivitiesRequest.getPrizeId() + "");
        rechargeRequest.setOrderSign("NEWACTIVITIES");
        rechargeRequest.setMemberId(newActivitiesRequest.getMemberId());
        rechargeRequest.setAppType(newActivitiesRequest.getAppType());
        String orderNo = iNewActivitiesService.createRechargeOrder(rechargeRequest);
        String chargeStr = PingppUtil.getChargeCommon(orderNo, newActivitiesRequest.getAmount() + "", newActivitiesRequest.getPayType(),"",newActivitiesRequest.getAppSign());
        if (!StringUtils.isEmpty(chargeStr)) {
            JsonResult result = new JsonResult();
            result.setData(chargeStr);
            return result;
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }

}
